package com.link.gateway;

import com.link.gateway.core.filter.local.LinkTestFilter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@MapperScan(basePackages = "com.link.**.mapper")
@EnableFeignClients("com.link.**.microservice")
public class CloudgatewayApplication {


    public static void main(String[] args) {
        SpringApplication.run(CloudgatewayApplication.class, args);
    }

    //具有负载功能的restTemplate
    @Bean
    @Primary
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }


//    @Bean
    public RouteLocator routes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("linkcrm-login", r -> r.path("/linkcrm/base/user/login")
                        .filters(f -> f.stripPrefix(1).filter(new LinkTestFilter()))
                        .uri("lb://link-base"))
                .build();
    }

    /**
     * @author guoshan
     * @date 2019/08/01
     * 生成路径匹配bean
     * @return/CacheBodyGatewayFilter.java
     */
    @Bean
    public AntPathMatcher getUrlPathMatcher() {
        return new AntPathMatcher();
    }

}
